package fr.mathis.tbbtmoments;

import java.util.Random;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

/**
 * An activity representing a single Quote detail screen. This activity is only
 * used on handset devices. On tablet-size devices, item details are presented
 * side-by-side with a list of items in a {@link QuoteListActivity}.
 * <p>
 * This activity is mostly just a 'shell' activity containing nothing more than
 * a {@link QuoteDetailFragment}.
 */
public class QuoteDetailActivity extends SherlockFragmentActivity implements SimpleGestureFilter.SimpleGestureListener{

	private static final int MENU_R = 6;
	private String mItemId = "";
	SimpleGestureFilter detector;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quote_detail);
		detector = new SimpleGestureFilter(this,this);
		// Show the Up button in the action bar.
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);


		// savedInstanceState is non-null when there is fragment state
		// saved from previous configurations of this activity
		// (e.g. when rotating the screen from portrait to landscape).
		// In this case, the fragment will automatically be re-added
		// to its container so we don't need to manually add it.
		// For more information, see the Fragments API guide at:
		//
		// http://developer.android.com/guide/components/fragments.html
		//
		if (savedInstanceState == null) {
			// Create the detail fragment and add it to the activity
			// using a fragment transaction.
			Bundle arguments = new Bundle();
			mItemId = getIntent().getStringExtra(QuoteDetailFragment.ARG_ITEM_ID);
			arguments.putString(QuoteDetailFragment.ARG_ITEM_ID, getIntent().getStringExtra(QuoteDetailFragment.ARG_ITEM_ID));
			QuoteDetailFragment fragment = new QuoteDetailFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction().add(R.id.quote_detail_container, fragment).commit();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			Intent intent=new Intent();
			intent.putExtra("RESULT_STRING", mItemId);
			setResult(RESULT_OK,intent);
			finish();
			//NavUtils.navigateUpTo(this, new Intent(this,QuoteListActivity.class));
			return true;
		case MENU_R:
			RandomChoice();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_R, 0, R.string.shuffle).setIcon(R.drawable.av_shuffle).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		return true;
	}

	private void RandomChoice() {
		Random r = new Random();
		int pos = r.nextInt(StateOlder.getInstance().getItems().size());

		Intent detailIntent = new Intent(this, QuoteDetailActivity.class);
		detailIntent.putExtra(QuoteDetailFragment.ARG_ITEM_ID, StateOlder.getInstance().getItems().get(pos).id);
		startActivityForResult(detailIntent, 0);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			String id = data.getStringExtra("RESULT_STRING");
			Intent intent=new Intent();
			intent.putExtra("RESULT_STRING", id);
			setResult(RESULT_OK, intent);
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	public void onBackPressed() {
		Intent intent=new Intent();
		intent.putExtra("RESULT_STRING", mItemId);
		setResult(RESULT_OK,intent);
		super.onBackPressed();
	}
	
	@Override 
	public boolean dispatchTouchEvent(MotionEvent me){ 
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me); 
	}

	@Override
	public void onSwipe(int direction) {

		int c=0;
		switch (direction) {

		case SimpleGestureFilter.SWIPE_RIGHT :
			
			for(int i = 0 ; i < StateOlder.getInstance().getItems().size() ; i++)
			{
				if(StateOlder.getInstance().getItems().get(i).id.compareTo(mItemId)==0)
				{
					c=i;
					break;
				}
			}
			if(c==0)
			{
				c = StateOlder.getInstance().getItems().size()-1;
			}
			else {
				c--;
			}
			Intent detailIntent = new Intent(this, QuoteDetailActivity.class);
			detailIntent.putExtra(QuoteDetailFragment.ARG_ITEM_ID, StateOlder.getInstance().getItems().get(c).id);
			startActivityForResult(detailIntent, 0);
			overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
			
		break;
		case SimpleGestureFilter.SWIPE_LEFT : 

			for(int i = 0 ; i < StateOlder.getInstance().getItems().size() ; i++)
			{
				if(StateOlder.getInstance().getItems().get(i).id.compareTo(mItemId)==0)
				{
					c=i;
					break;
				}
			}
			if(c==(StateOlder.getInstance().getItems().size()-1))
			{
				c = 0;
			}
			else {c++;}
			Intent detailIntent2 = new Intent(this, QuoteDetailActivity.class);
			detailIntent2.putExtra(QuoteDetailFragment.ARG_ITEM_ID, StateOlder.getInstance().getItems().get(c).id);
			startActivityForResult(detailIntent2, 0);
			overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			
		break;

		} 

	}

	@Override
	public void onDoubleTap() {
		
	}
}
